#!/usr/bin/env bash
set -euo pipefail

# doc-edit.sh: Enable IDE-friendly editing of DOC/DOCX via Markdown roundtrip.
# Usage:
#   ./tools/doc-edit.sh to-md <path/to/file.docx|file.doc>
#   ./tools/doc-edit.sh to-docx <path/to/file.md> [--to-doc]
#   ./tools/doc-edit.sh roundtrip <path/to/file.docx|file.doc>
# Notes:
# - Images are extracted to assets/<basename>/ when converting to Markdown.
# - On macOS, legacy .doc is converted to .docx using textutil before processing.

red() { printf "\033[31m%s\033[0m\n" "$*"; }
green() { printf "\033[32m%s\033[0m\n" "$*"; }
yellow() { printf "\033[33m%s\033[0m\n" "$*"; }

need_cmd() {
  if ! command -v "$1" >/dev/null 2>&1; then
    red "Missing dependency: $1"; return 1
  fi
}

to_md() {
  local src="$1"
  if [[ ! -f "$src" ]]; then
    red "Source file not found: $src"; exit 1
  fi

  local ext="${src##*.}"
  local base
  base=$(basename "$src")
  local stem="${base%.*}"
  local dir
  dir=$(dirname "$src")

  need_cmd pandoc || {
    yellow "Install pandoc first: brew install pandoc"; exit 1
  }

  local docx_src="$src"
  if [[ "$ext" == "doc" ]]; then
    need_cmd textutil || { red "textutil not available on this system"; exit 1; }
    docx_src="$dir/$stem.docx"
    yellow "Converting legacy .doc to .docx: $src -> $docx_src"
    textutil -convert docx "$src" -output "$docx_src"
  fi

  local out_md="$dir/$stem.md"
  local media_dir="assets/$stem"
  mkdir -p "$dir/$media_dir"
  yellow "Converting to Markdown with media extraction: $docx_src -> $out_md"
  pandoc -s "$docx_src" -t gfm --extract-media="$media_dir" -o "$out_md"
  green "Generated: $out_md"
}

to_docx() {
  local src_md="$1"
  local to_doc="false"
  if [[ "${2:-}" == "--to-doc" ]]; then
    to_doc="true"
  fi

  if [[ ! -f "$src_md" ]]; then
    red "Markdown file not found: $src_md"; exit 1
  fi

  need_cmd pandoc || { yellow "Install pandoc: brew install pandoc"; exit 1; }

  local base
  base=$(basename "$src_md")
  local stem="${base%.*}"
  local dir
  dir=$(dirname "$src_md")

  local out_docx="$dir/$stem.docx"
  yellow "Converting Markdown to DOCX: $src_md -> $out_docx"
  pandoc -s "$src_md" -o "$out_docx"
  green "Generated: $out_docx"

  if [[ "$to_doc" == "true" ]]; then
    need_cmd textutil || { red "textutil not available to produce .doc"; exit 1; }
    local out_doc="$dir/$stem.doc"
    yellow "Converting DOCX to legacy .doc: $out_docx -> $out_doc"
    textutil -convert doc "$out_docx" -output "$out_doc"
    green "Generated: $out_doc"
  fi
}

roundtrip() {
  local src="$1"
  to_md "$src"
  local base
  base=$(basename "$src")
  local stem="${base%.*}"
  local dir
  dir=$(dirname "$src")
  to_docx "$dir/$stem.md"
}

usage() {
  cat <<EOF
Usage:
  $0 to-md <path/to/file.docx|file.doc>
  $0 to-docx <path/to/file.md> [--to-doc]
  $0 roundtrip <path/to/file.docx|file.doc>
Examples:
  $0 to-md "仓库管理规范与流程手册v2.5/《2.1.3》SIM卡智能读卡器操作指南V1.0.docx"
  $0 to-docx "仓库管理规范与流程手册v2.5/《2.1.3》SIM卡智能读卡器操作指南V1.0.md"
  $0 to-docx ".../指南.md" --to-doc  # also produce legacy .doc
EOF
}

cmd="${1:-}"
case "$cmd" in
  to-md)
    shift; to_md "$@" ;;
  to-docx)
    shift; to_docx "$@" ;;
  roundtrip)
    shift; roundtrip "$@" ;;
  *)
    usage; exit 1 ;;
esac

